package server

import (
	"github.com/gin-gonic/gin"
	"github.com/spf13/viper"
	swaggerfiles "github.com/swaggo/files"
	ginSwagger "github.com/swaggo/gin-swagger"
	apiV1 "golang-sgg-daijia/api/v1"
	"golang-sgg-daijia/docs"
	"golang-sgg-daijia/internal/handler"
	"golang-sgg-daijia/internal/handler/customer"
	"golang-sgg-daijia/internal/handler/driver"
	"golang-sgg-daijia/internal/middleware"
	"golang-sgg-daijia/internal/repository/redis"
	"golang-sgg-daijia/pkg/jwt"
	"golang-sgg-daijia/pkg/log"
	"golang-sgg-daijia/pkg/server/http"
)

func NewHTTPServer(
	logger *log.Logger,
	conf *viper.Viper,
	jwt *jwt.JWT,

	customerHandler *customer.CustomerHandler,
	couponHandler *customer.CouponHandler,
	customerOrderHandler *customer.OrderHandler,

	driverHandler *driver.DriverHandler,
	cosHandler *driver.CosHandler,
	fileHandler *driver.FileHandler,
	locationHandler *driver.LocationHandler,
	monitorHandler *driver.MonitorHandler,
	ocrHandler *driver.OcrHandler,
	driverOrderHandler *driver.OrderHandler,

	callbackHandler *handler.CallbackHandler,

	redisCli redis.RedisClient,
) *http.Server {
	gin.SetMode(gin.DebugMode)
	s := http.NewServer(
		gin.Default(),
		logger,
		http.WithServerHost(conf.GetString("http.host")),
		http.WithServerPort(conf.GetInt("http.port")),
	)

	// swagger doc
	docs.SwaggerInfo.BasePath = "/v1"
	s.GET("/swagger/*any", ginSwagger.WrapHandler(
		swaggerfiles.Handler,
		//ginSwagger.URL(fmt.Sprintf("http://localhost:%d/swagger/doc.json", conf.GetInt("app.http.port"))),
		ginSwagger.DefaultModelsExpandDepth(-1),
		ginSwagger.PersistAuthorization(true),
	))

	s.Use(
		middleware.CORSMiddleware(),
		middleware.ResponseLogMiddleware(logger),
		middleware.RequestLogMiddleware(logger),
		//middleware.SignMiddleware(log),
	)
	s.GET("/", func(ctx *gin.Context) {
		logger.WithContext(ctx).Info("hello")
		apiV1.HandleSuccess(ctx, map[string]interface{}{
			":)": "Thank you for using nunu!",
		})
	})

	customerWebGroup := s.Group("/customer-api")
	{
		noAuthCustomerGroup := customerWebGroup.Group("/")
		{
			noAuthCustomerGroup.GET("/customer/login/:code", customerHandler.Login)
		}
		// 需要登录
		customerGroup := customerWebGroup.Group("/customer").Use(middleware.UserLoginAuth(logger, redisCli))
		{
			customerGroup.GET("/getCustomerLoginInfo", customerHandler.GetCustomerLoginInfo)
			customerGroup.POST("/updateWxPhone", customerHandler.UpdateWxPhoneNumber)
		}
		couponGroup := customerWebGroup.Group("/coupon").Use(middleware.UserLoginAuth(logger, redisCli))
		{
			couponGroup.GET("/findNoReceivePage/:page/:limit", couponHandler.FindNoReceivePage)
			couponGroup.GET("/findNoUsePage/:page/:limit", couponHandler.FindNoUsePage)
			couponGroup.GET("/receive/:couponId", couponHandler.Receive)
			couponGroup.GET("/findAvailableCoupon/:orderId", couponHandler.FindAvailableCoupon)
			couponGroup.GET("/findUsedPage/:page/:limit", couponHandler.FindUsedPage)
		}
		orderGroup := customerWebGroup.Group("/order").Use(middleware.UserLoginAuth(logger, redisCli))
		{
			orderGroup.GET("searchCustomerCurrentOrder", customerOrderHandler.SearchCustomerCurrentOrder)
			orderGroup.POST("/expectOrder", customerOrderHandler.ExpectOrder)
			orderGroup.POST("/submitOrder", customerOrderHandler.SubmitOrder)
			orderGroup.GET("/getOrderStatus/:orderId", customerOrderHandler.GetOrderStatus)
			orderGroup.GET("/getOrderInfo/:orderId", customerOrderHandler.GetOrderInfo)
			orderGroup.GET("/getDriverInfo/:orderId", customerOrderHandler.GetDriverInfo)
			orderGroup.GET("/getCacheOrderLocation/:orderId", customerOrderHandler.GetCacheOrderLocation)
			orderGroup.POST("/calculateDrivingLine", customerOrderHandler.CalculateDrivingLine)
			orderGroup.GET("/getOrderServiceLastLocation/:orderId", customerOrderHandler.GetOrderServiceLastLocation)
			orderGroup.GET("/findCustomerOrderPage/:page/:limit", customerOrderHandler.FindCustomerOrderPage)
			orderGroup.POST("/createWxPayment", customerOrderHandler.CreateWxPayment)
			orderGroup.GET("/queryPayStatus/:orderNo", customerOrderHandler.QueryPayStatus)
		}
	}

	driverWebGroup := s.Group("/driver-api")
	{
		noAuthDriverGroup := driverWebGroup.Group("/")
		{
			noAuthDriverGroup.GET("/driver/login/:code", driverHandler.Login)

		}
		// 需要登录
		driverGroup := driverWebGroup.Group("/driver").Use(middleware.UserLoginAuth(logger, redisCli))
		{
			driverGroup.GET("/getDriverLoginInfo", driverHandler.GetDriverLoginInfo)
			driverGroup.GET("/getDriverAuthInfo", driverHandler.GetDriverAuthInfo)
			driverGroup.POST("/updateDriverAuthInfo", driverHandler.UpdateDriverAuthInfo)
			driverGroup.POST("/creatDriverFaceModel", driverHandler.CreateDriverFaceModel)
			driverGroup.GET("/isFaceRecognition", driverHandler.IsFaceRecognition)
			driverGroup.POST("/verifyDriverFace", driverHandler.VerifyDriverFace)
			driverGroup.GET("/startService", driverHandler.StartService)
			driverGroup.GET("/stopService", driverHandler.StopService)
		}
		cosGroup := driverWebGroup.Group("/cos").Use(middleware.UserLoginAuth(logger, redisCli))
		{
			cosGroup.POST("/upload", cosHandler.Upload)
		}

		fileGroup := driverWebGroup.Group("/file")
		{
			fileGroup.POST("/upload", fileHandler.Upload)
		}
		locationGroup := driverWebGroup.Group("/location").Use(middleware.UserLoginAuth(logger, redisCli))
		{
			locationGroup.POST("/updateDriverLocation", locationHandler.UpdateDriverLocation)
			locationGroup.POST("/updateOrderLocationToCache", locationHandler.UpdateOrderLocationToCache)
			locationGroup.POST("/saveOrderServiceLocation", locationHandler.SaveOrderServiceLocation)
		}
		MonitorGroup := driverWebGroup.Group("/monitor").Use(middleware.UserLoginAuth(logger, redisCli))
		{
			MonitorGroup.POST("/upload", monitorHandler.Upload)
		}
		ocrGroup := driverWebGroup.Group("/ocr").Use(middleware.UserLoginAuth(logger, redisCli))
		{
			ocrGroup.POST("/idCardOcr", ocrHandler.UploadDriverLicenseOcr)
			ocrGroup.POST("/driverLicenseOcr", ocrHandler.DriverLicenseOcr)
		}
		orderGroup := driverWebGroup.Group("/order").Use(middleware.UserLoginAuth(logger, redisCli))
		{
			orderGroup.GET("/getOrderStatus/:orderId", driverOrderHandler.GetOrderStatus)
			orderGroup.GET("/findNewOrderQueueData", driverOrderHandler.FindNewOrderQueueData)
			orderGroup.GET("/robNewOrder/:orderId", driverOrderHandler.RobNewOrder)
			orderGroup.GET("/searchDriverCurrentOrder", driverOrderHandler.SearchDriverCurrentOrder)
			orderGroup.GET("/getOrderInfo/:orderId", driverOrderHandler.GetOrderInfo)
			orderGroup.POST("/calculateDrivingLine", driverOrderHandler.CalculateDrivingLine)
			orderGroup.GET("/driverArriveStartLocation/:orderId", driverOrderHandler.DriverArriveStartLocation)
			orderGroup.POST("/updateOrderCart", driverOrderHandler.UpdateOrderCart)
			orderGroup.POST("/startDrive", driverOrderHandler.StartDrive)
			orderGroup.POST("/endDrive", driverOrderHandler.EndDrive)
			orderGroup.GET("/findDriverOrderPage/:page/:limit", driverOrderHandler.FindDriverOrderPage)
			orderGroup.GET("/sendOrderBillInfo/:orderId", driverOrderHandler.SendOrderBillInfo)

		}
	s.POST("/payment/wxPay/notify", callbackHandler.Notify)

	}


	return s
}
